﻿package test;

import java.util.LinkedList;

import html.ShortText;
import lib.dbTool.*;
import test.*;
import my.item.*;
import my.gridView.*;

public class ScoreTableBean extends GridModel {

	public ScoreTableBean() {
		super(new DefaultJNumberList());
	}

	// ---------------------------------
	private String scoreid;

	public String getScoreid() {
		return scoreid;
	}

	public void setScoreid(String scoreid) {
		this.scoreid = scoreid;
	}

	// ---------------------------------
	private String classname;

	public String getClassname() {
		return classname;
	}

	public void setClassname(String classname) {
		this.classname = classname;
	}

	// ---------------------------------
	private String score;

	public String getScore() {
		return score;
	}

	public void setScore(String score) {
		this.score = score;
	}

	// ---------------------------------
	private String studentid;

	public String getStudentid() {
		return studentid;
	}

	public void setStudentid(String studentid) {
		this.studentid = studentid;
	}

	// ---------------------------------
	// ---------------------------------
	private String classnameAdd;

	public String getClassnameAdd() {
		return classnameAdd;
	}

	public void setClassnameAdd(String classnameAdd) {
		this.classnameAdd = classnameAdd;
	}

	// ---------------------------------
	private String scoreAdd;

	public String getScoreAdd() {
		return scoreAdd;
	}

	public void setScoreAdd(String scoreAdd) {
		this.scoreAdd = scoreAdd;
	}

	// ---------------------------------
	private String studentidAdd;

	public String getStudentidAdd() {
		return studentidAdd;
	}

	public void setStudentidAdd(String studentidAdd) {
		this.studentidAdd = studentidAdd;
	}

	// ---------------------------------

	private String addErr;	
	public String getAddErr() {
		return addErr;
	}

	public void setAddErr(String addErr) {
		this.addErr = addErr;
	}
	// ---------------------------------
	private int showAlert;	
	public int getShowAlert() {
		return showAlert;
	}

	public void setShowAlert(int showAlert) {
		this.showAlert = showAlert;
	}
	
	
	/**
	 * 自訂事件
	 */
	@Override
	public void jaddListener() {
		// 指定PK
		this.scoreid = pk;
		// 新增一筆資料利用 【自訂事件】
		// this.addJMyListener(new JMyListener() {
		//
		// @Override
		// public String getMyCommandValue() {
		// return "add";
		// }
		//
		// @Override
		// public void action(GridModel gridModel) {
		// jAdd();
		// }
		// });
	}

	@Override
	public void jedit() {

	}

	@Override
	public void jdel() {
		SCORE x = new SCORE();
		int int_scoreid = Integer.parseInt(pk);
		int scoreidInt = Integer.parseInt(scoreid);
		int row = x.delete(scoreidInt);
		
		if(row == 0){
			showAlert=0;
		}
		else{
			showAlert=1;
		}
	}

	@Override
	public void jyes() {
		SCORE x = new SCORE();
		int int_scoreid = Integer.parseInt(pk);
		int scoreInt = Integer.parseInt(score);
		int studentidInt = Integer.parseInt(studentid);
		int scoreidInt = Integer.parseInt(scoreid);
		int row = x.update(classname, scoreInt, studentidInt, scoreidInt);
	}

	@Override
	public void jno() {

	}

	@Override
	public void jadd() {
		SCORE x = new SCORE();
		try {
			int scoreAddInt = Integer.parseInt(scoreAdd);
			int studentidAddInt = Integer.parseInt(studentidAdd);
			int row = x.insert(classnameAdd, scoreAddInt, studentidAddInt);
			System.out.println("新增結果 = " + row);
			
		} catch (Exception e) {
			addErr = "新增錯誤";
			// TODO: handle exception
		}
		// 新增一筆後, 要重新計算頁數, 然後再指定到最後一頁
		totalPage();
		this.toPage = this.totalPage;
	}

	@Override
	public void sortData(String fieldName, int sortNowValue) {
		// TODO Auto-generated method stub

	}

	/**
	 * 找出共有幾頁
	 */
	@Override
	public void totalPage() {
		// TODO Auto-generated method stub
		SCORE x = new SCORE();
		rowCount = x.selectCount();
		totalPage = rowCount / pageSize;
		totalPage += rowCount % pageSize == 0 ? 0 : 1;
	}

	/**
	 * select 分頁出來的內容
	 */
	@Override
	public TableData selectPage(int pageSize, int pageIndex) {
		// TODO Auto-generated method stub
		SCORE x = new SCORE();
		// ------------新增
		if (pageIndex <= 0) {
			pageIndex = 1;
		}

		// this.sortNowName : 我按到哪一欄位
		// this.sortNowValue : 排序方向
		// 0=不排序, 1=正向, 2=反向

		String db[][] = null;
		String sortName;
		int desc;

		// 以下處理排序問題
		sortName = this.sortNowName;
		if (this.sortNowValue == 0) {
			db = x.selectPage(pageSize, pageIndex, rowCount);
		} else {
			if (this.sortNowValue == 1) {
				desc = PaggingSQL.ASC;
			} else {
				desc = PaggingSQL.DESC;
			}
			db = x.selectPageOrderBy(pageSize, pageIndex, sortName, desc,
					rowCount);
		}
		// ------------新增
		TableData t = new TableData();
		// keyIndex 要自己根據 db 來決定
		int keyIndex = 0;
		// fake 為 db select 出來的 String[][]
		t.setData(keyIndex, db);
		return t;
	}

	/**
	 * 非編輯模式的資料
	 */
	@Override
	public Cell[] loadLabel(TableData t, TempCell tc) {
		// TODO Auto-generated method stub
		String datas[][] = t.getDatas();
		// 表示select db 出來的欄位數量
		int colSize = 4;
		Cell tmpCell[] = new Cell[colSize];
		// 第一欄預設為 [CLabel], 若修改為其它的 class 請參考 [CLabel], showYesNo() = false
		// getValue() 要傳回值[純PK],不可有<input>,第一欄一定為 PK
		// 若不想 show 出來, getHtmlItem()可以在control.jsp 中刪除
		tmpCell[0] = new CLabel("scoreid");
		tmpCell[1] = new CLabel("classname");
		tmpCell[2] = new CLabel("score");
		tmpCell[3] = new CLabel("studentid");

		tc.loadCell(tmpCell, datas);
		return tmpCell;
	}

	/**
	 * 進入編輯模式的資料
	 */
	@Override
	public Cell[] loadItem(TableData t, TempCell tc) {
		// TODO Auto-generated method stub
		String datas[][] = t.getDatas();
		int colSize = 4;
		Cell tmpCell[] = new Cell[colSize];
		// 第一欄預設為 [CLabelForUpdate][CLabelHidden], 若修改為其它的 class 請參考
		// [CLabelForUpdate][CLabelHidden]
		// , showYesNo() = true, getValue() 要傳回值[純PK],不可有<input>, 第一欄一定為 PK
		// 若不想 show 出來, getHtmlItem()可以在control.jsp 中刪除
		tmpCell[0] = new CLabelForUpdate("scoreid");
		tmpCell[1] = new CDropList("classname",new IDropListData() {	
			LinkedList<String> list = new LinkedList<String>();
			{
				list.add("日文");
				list.add("英文");
				list.add("國文");
				list.add("音樂");
			}
			@Override
			public LinkedList<String> loadName() {
				// TODO Auto-generated method stub
				return list;
			}
			
			@Override
			public LinkedList<String> loadData() {
				// TODO Auto-generated method stub
				return list;
			}
		});
		
		tmpCell[2] = new ShortText("score");
		tmpCell[3] = new CText("studentid");

		tc.loadCell(tmpCell, datas);
		return tmpCell;
	}

	
	
}
